package com.example.mobile1931605066wzq0529;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private WebView wvMainList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        wvMainList.loadUrl("file:////android_asset/index.html");
        wvMainList.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });
        wvMainList.getSettings().setJavaScriptEnabled(true);
        wvMainList.addJavascriptInterface(new LoginJsInterface(), "obj");

    }

    class LoginJsInterface {
        @JavascriptInterface
        public void  login(String name,String password) {
            if (name.equals("admin")&&password.equals("123456")){
                startActivity(new Intent(MainActivity.this,NewsActivity.class));
            }else {
                Toast.makeText(MainActivity.this,"登陆失败",Toast.LENGTH_LONG)
                        .show();
            }

        }
    }

    private void initView() {

        wvMainList = (WebView) findViewById(R.id.wvMainList);
    }
}
